package xyz.jpcr.covid19app;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import androidx.navigation.NavGraph;
import androidx.navigation.NavGraphNavigator;
import androidx.navigation.Navigation;
import androidx.navigation.NavigatorProvider;
import androidx.navigation.fragment.FragmentNavigator;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.google.android.material.navigation.NavigationView;

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import xyz.jpcr.covid19app.Api.ApiClient;
import xyz.jpcr.covid19app.Fragment.CollectionFragment;
import xyz.jpcr.covid19app.Fragment.CountryFragment;
import xyz.jpcr.covid19app.Fragment.FixFragmentNavigator;
import xyz.jpcr.covid19app.Fragment.OverallFragment;
import xyz.jpcr.covid19app.Fragment.RefuteRumorsFragment;
import xyz.jpcr.covid19app.Model.Country.Country;
import xyz.jpcr.covid19app.Util.ILocationUtil;
import xyz.jpcr.covid19app.Util.IPermissionUtil;

public class MainActivity extends AppCompatActivity {
    DrawerLayout drawerLayout;
    NavigationView navigationView;
    Toolbar toolbar;
    long lastTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IPermissionUtil.getInstance().initPermission(this); // 获取权限

        drawerLayout = findViewById(R.id.drawerLayout);
        navigationView = findViewById(R.id.navigationView);
        toolbar = findViewById(R.id.layoutToolBar);

        navigationView.setItemIconTintList(null);

        setSupportActionBar(toolbar);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });


        Fragment fragmentById = getSupportFragmentManager().findFragmentById(R.id.navHostFragment);
        //fragment的重复加载问题和NavController有关
        final NavController navController = NavHostFragment.findNavController(fragmentById);
        NavigatorProvider provider = navController.getNavigatorProvider();
        //设置自定义的navigator
        FixFragmentNavigator fixFragmentNavictor = new FixFragmentNavigator(this, fragmentById.getChildFragmentManager(), fragmentById.getId());
        provider.addNavigator(fixFragmentNavictor);

        NavGraph navDestinations = initNavGraph(provider, fixFragmentNavictor);
        navController.setGraph(navDestinations);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) {
                navController.navigate(item.getItemId());
                if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                    drawerLayout.closeDrawer(GravityCompat.START);
                }
                if(item.getItemId() == R.id.menuOverall) {
                    ((AppCompatActivity) fragmentById.requireActivity()).getSupportActionBar().setTitle(MainActivity.this.getText(R.string.covid_19_tracking) + "(Overall)");
                } else if(item.getItemId() == R.id.menuCountry) {
                    if(ILocationUtil.getInstance(MainActivity.this).getCity() != null ) {
                        ((AppCompatActivity) fragmentById.requireActivity()).getSupportActionBar().setTitle(MainActivity.this.getText(R.string.covid_19_tracking) + "(" +ILocationUtil.getInstance(MainActivity.this).getCity() + ")");
                    }
                } else if(item.getItemId() == R.id.menuRefuteRumors) {
                    ((AppCompatActivity) fragmentById.requireActivity()).getSupportActionBar().setTitle("Refute Rumors");
                } else if(item.getItemId() == R.id.menuCollection) {
                    ((AppCompatActivity) fragmentById.requireActivity()).getSupportActionBar().setTitle("Collections");
                }
                return true;
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) { // 如果当前抽屉布局打开，按下返回键就是关闭抽屉布局
            drawerLayout.closeDrawer(GravityCompat.START);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - lastTime) > 2000) {
                Toast toast = Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT);
                toast.setText(MainActivity.this.getString(R.string.quit_app));
                toast.show(); // 生成不带应用名的提示
                lastTime = System.currentTimeMillis();
            } else {
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private NavGraph initNavGraph(NavigatorProvider provider, FixFragmentNavigator fragmentNavigator) {
        NavGraph navGraph = new NavGraph(new NavGraphNavigator(provider));

        //用自定义的导航器来创建目的地
        FragmentNavigator.Destination destination1 = fragmentNavigator.createDestination();
        destination1.setId(R.id.menuCountry);
        destination1.setClassName(CountryFragment.class.getCanonicalName());
        navGraph.addDestination(destination1);


        FragmentNavigator.Destination destination2 = fragmentNavigator.createDestination();
        destination2.setId(R.id.menuCollection);
        destination2.setClassName(CollectionFragment.class.getCanonicalName());
        navGraph.addDestination(destination2);

        FragmentNavigator.Destination destination3 = fragmentNavigator.createDestination();
        destination3.setId(R.id.menuOverall);
        destination3.setClassName(OverallFragment.class.getCanonicalName());
        navGraph.addDestination(destination3);


        FragmentNavigator.Destination destination4 = fragmentNavigator.createDestination();
        destination4.setId(R.id.menuRefuteRumors);
        destination4.setClassName(RefuteRumorsFragment.class.getCanonicalName());
        navGraph.addDestination(destination4);

        navGraph.setStartDestination(destination1.getId());

        return navGraph;
    }


}

